package dao.impl;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import dao.GenericDao;

public class GenericDaoImpl<T,ID extends Serializable> extends HibernateDaoSupport implements GenericDao<T, ID>{

	private Class<T> entityClass;
	
	
	
	@SuppressWarnings("unchecked")
	public GenericDaoImpl() {
		this.entityClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
	}

	@SuppressWarnings("unchecked")
	@Override
	public ID save(T t) {
		// TODO Auto-generated method stub
		return (ID) getHibernateTemplate().save(t);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<T> queryAll() {
		// TODO Auto-generated method stub
		System.out.println("in GenericDaoImpl queryAll"+entityClass.getName());
		String queryString="from "+entityClass.getName();
		return getHibernateTemplate().find(queryString);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<T> queryByHql(String queryString) {
		// TODO Auto-generated method stub
		return getHibernateTemplate().find(queryString);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<T> queryByNamedParam(String queryString, String[] paramNames,
			Object[] values) {
		// TODO Auto-generated method stub
		return getHibernateTemplate().findByNamedParam(queryString, paramNames, values);
	}

	@Override
	public T queryById(ID id) {
		// TODO Auto-generated method stub
		return getHibernateTemplate().get(entityClass, id);
	}

	@Override
	public void delete(ID id) {
		// TODO Auto-generated method stub
		getHibernateTemplate().delete(id);
	}

	@Override
	public void delete(T t) {
		// TODO Auto-generated method stub
		getHibernateTemplate().delete(t);
	}

	@Override
	public void update(T t) {
		// TODO Auto-generated method stub
		getHibernateTemplate().update(t);
	}

	@Override
	public void saveOrUpdate(T t) {
		// TODO Auto-generated method stub
		getHibernateTemplate().saveOrUpdate(t);
	}

	@Override
	public void merge(T t) {
		// TODO Auto-generated method stub
		getHibernateTemplate().merge(t);
	}

	@Override
	public Session getS() {
		// TODO Auto-generated method stub
		return super.getSession();
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<T> queryForPage( final String queryString,final int offset,final int length) {
		// TODO Auto-generated method stub
		return (List<T>) getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session) throws HibernateException,SQLException{
				return session.createQuery(queryString).setFirstResult(offset).setMaxResults(length).list();
			}
		});
	}

	@Override
	public int getAllRowCount(String queryString) {
		// TODO Auto-generated method stub
		return getHibernateTemplate().find(queryString).size();
	}

}
